package pers.cj.pdos.controller;


import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.GoodsTemplate;
import pers.cj.pdos.entity.Packet;
import pers.cj.pdos.entity.PacketGoods;
import pers.cj.pdos.service.IPacketService;
import pers.cj.pdos.utils.DataUtil;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.PacketSaleVo;
import pers.cj.pdos.vo.PacketVo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/marketGoods")
public class PacketController extends BaseController {
    @Autowired
    IPacketService packetService;
    /**
     * 商品套餐
     * @return
     */
    @RequestMapping("/packet")
    @RequiresPermissions("packet:query")
    public String goodsPacket() {
        return "marketGoods/packet";
    }

    /**
     * 套餐新增
     * @return
     */
    @RequestMapping("/packetNew")
    @RequiresPermissions("packet:insert")
    public String goodsPacketNew() {
        return "marketGoods/packetNew";
    }

    /**
     * 套餐编辑
     * @return
     */
    @RequestMapping("/packetEdit")
    @RequiresPermissions("packet:insert")
    public String goodsPacketEdit(Model model, String id) {
        model.addAttribute("id",id);
        boolean used = packetService.check(id);

        model.addAttribute("used",used);
        return "marketGoods/packetEdit";
    }

    /**
     * 套餐明细
     * @return
     */
    @RequestMapping("/packetDetail")
    @RequiresPermissions("packet:query")
    public String goodsPacketDetail(Model model, String id) {
        model.addAttribute("id",id);
        return "marketGoods/packetDetail";
    }

    /**
     * 销售明细
     * @return
     */
    @RequestMapping("/packetSaleDetail")
    @RequiresPermissions("packet:query")
    public String goodsPacketSaleDetail(Model model, String id) {
        model.addAttribute("id",id);
        return "marketGoods/packetSaleDetail";
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/getSaleListById")
    public ResponseList getListSale(int page,
                                    int limit,
                                    String close,
                                    String search,
                                    String packetId,
                                    String storeId,
                                    String userId,
                                    String date1,
                                    String date2) {
        Company company = getCompany();
        List<PacketSaleVo> list = packetService.listSaleByPacketId(page,
                limit,
                Utils.genBoolean(close),
                Utils.genLikeData(search),
                packetId,
                storeId,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2));

        long count = packetService.countSaleByPacketId(Utils.genBoolean(close),
                Utils.genLikeData(search),
                packetId,
                storeId,
                userId,
                Utils.genDate(date1),
                Utils.genDate(date2));
        return ResponseList.success(count,list);
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/getListWithStock")
    public ResponseList getListWithStock(int page, int limit, String status, String searchContent,String houseId) {
        if (Utils.isEmpty(houseId)) {
            return ResponseList.fail("输入参数错误");
        }
        Company company = getCompany();
        List<PacketVo> list = packetService.listVoWithStock(company.getId(), page,limit,Utils.genInteger(status),Utils.genLikeData(searchContent),houseId);
        return ResponseList.success(packetService.count(company.getId(),Utils.genInteger(status),Utils.genLikeData(searchContent)),list);
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/getList")
    public ResponseList getList(int page, int limit, String status, String searchContent) {
        Company company = getCompany();
        List<PacketVo> list = packetService.listVoPage(company.getId(), page,limit,Utils.genInteger(status),Utils.genLikeData(searchContent));
        return ResponseList.success(packetService.count(company.getId(),Utils.genInteger(status),Utils.genLikeData(searchContent)),list);
    }

    @ResponseBody
    @RequiresPermissions("packet:insert")
    @RequestMapping("/goodsPacket/add")
    public ResponseEntity add(@RequestBody PacketVo packetVo){
        Packet packet = DataUtil.genEntityFromVo(Packet.class, packetVo);
        List<PacketGoods> packetGoodsList = packetVo.getPacketGoodsList();
        if (Utils.isEmpty(packet.getName())){
            return ResponseEntity.fail("套餐名称不能为空");
        }
        if (Utils.isEmpty(packet.getNo())){
            return ResponseEntity.fail("套餐编号不能为空");
        }
        if (Utils.isEmpty(packetGoodsList)){
            return ResponseEntity.fail("您未选择任何商品");
        }

        Company company = getCompany();
        if (packetService.checkName(packet.getName(),company.getId())){
            return ResponseEntity.fail("套餐名称已被使用");
        }

        if (packetService.checkNo(packet.getNo(),company.getId())){
            return ResponseEntity.fail("套餐编号已被使用");
        }

        DataUtil.processEntity(company,packet);

        Map<String,Object> map = new HashMap<>();
        map.put("packetId",packet.getId());
        DataUtil.processList(company,packetGoodsList,map);

        packetService.insert(packet,packetGoodsList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("packet:insert")
    @RequestMapping("/goodsPacket/update")
    public ResponseEntity update(@RequestBody PacketVo packetVo){
        Packet packet = DataUtil.genEntityFromVo(Packet.class, packetVo);
        List<PacketGoods> packetGoodsList = packetVo.getPacketGoodsList();
        if (Utils.isEmpty(packet.getName())){
            return ResponseEntity.fail("套餐名称不能为空");
        }
        if (Utils.isEmpty(packet.getNo())){
            return ResponseEntity.fail("套餐编号不能为空");
        }
        if (Utils.isEmpty(packetGoodsList)){
            return ResponseEntity.fail("您未选择任何商品");
        }

        Company company = getCompany();

        Packet old = packetService.getById(packet.getId());
        if (!old.getName().equals(packet.getName()) && packetService.checkName(packet.getName(),company.getId())){
            return ResponseEntity.fail("套餐名称已被使用");
        }

        if (!old.getNo().equals(packet.getNo()) && packetService.checkNo(packet.getNo(),company.getId())){
            return ResponseEntity.fail("套餐编号已被使用");
        }

        DataUtil.processEntity(false, company,packet);

        Map<String,Object> map = new HashMap<>();
        map.put("packetId",packet.getId());
        DataUtil.processList(company,packetGoodsList,map);

        packetService.update(packet,packetGoodsList);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/delete")
    public ResponseEntity delete(@RequestBody Packet packet){
        if (packetService.check(packet.getId())){
            return ResponseEntity.fail("已被单据使用，无法删除");
        }
        packetService.delete(packet);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/getVoById")
    public ResponseData getVoById(String id){
        PacketVo packetVo = packetService.getVoById(id);
        return ResponseData.success(packetVo);
    }

    @ResponseBody
    @RequestMapping("/goodsPacket/listVoByIds")
    public ResponseData listVoByIds(@RequestBody List<String> ids){
        List<PacketVo> packetVoList = packetService.listVoByIds(ids);
        return ResponseData.success(packetVoList);
    }


}
